package com.atguigu.cloud.controller;

import cn.hutool.core.bean.BeanUtil;
import com.atguigu.cloud.entities.Pay;
import com.atguigu.cloud.entities.PayDTO;
import com.atguigu.cloud.resp.ResultData;
import com.atguigu.cloud.service.PayService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author liking
 * @Description
 * @Date 2025/4/23
 * @Version 1.0
 */
@RestController
@RequestMapping("/pay")
@Slf4j
@Tag(name = "支付接口", description = "支付接口curd")
public class PayController {
    @Resource
    private  PayService payService;

    @GetMapping("/getPayById/{id}")
    @Operation(summary = "根据id查询支付信息",description = "根据id查询支付信息")
    public ResultData<Pay> getPayById(@PathVariable("id") Integer id) {
        return ResultData.success(payService.getPayById(id));
    }

    @PostMapping("/createPay")
    @Operation(summary = "新增",description = "新增支付流水方法,json串做参数")
    public ResultData<String> createPay(@RequestBody PayDTO payDTO) {
        Pay pay = new Pay();
        BeanUtil.copyProperties(payDTO, pay);
        String createMsg = "创建失败";
        int i = payService.createPay(pay);
        if (i > 0) {
           createMsg = "创建成功";
        }
        return ResultData.success(createMsg);
    }

    @PostMapping("/updatePay")
    @Operation(summary = "修改",description = "修改支付流水方法")
    public ResultData<String> updatePay(@RequestBody PayDTO payDTO) {
        String updateMsg = "更新失败";
        Pay pay = new Pay();
        BeanUtil.copyProperties(payDTO, pay);
        if (payService.updatePay(pay) > 0) {
            updateMsg = "更新成功";
        }
       return ResultData.success(updateMsg);
    }
    @GetMapping("/deletePay/{id}")
    @Operation(summary = "删除",description = "删除支付流水方法")
    public ResultData<String> deletePay(@PathVariable("id") Integer id) {
        String deleteMsg = "删除失败";
        if (payService.deletePay(id) > 0) {
            deleteMsg = "删除成功";
        }
        return ResultData.success(deleteMsg);
    }

    @GetMapping("/getPayList")
    @Operation(summary = "查询所有",description = "查询所有")
    public ResultData<List<Pay>> getPayList() {
        return ResultData.success(payService.getPayList());

    }

    @GetMapping("/error")
    @Operation(summary = "自建异常",description = "自建异常")
    public ResultData<String> getPayListByUserId() {
        String msg = "";
        int i = Integer.parseInt(msg);
        return ResultData.success("字符串转Integer ：" + i);
    }
}
